package com.etc.view;

import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.util.ArrayList;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;

import com.etc.controller.SerializeOperation;
import com.etc.entity.Counter;
import com.etc.entity.Role;
import com.etc.service.RoleService;

public class FindCompetitor extends JFrame {

	JButton refresh = new JButton("刷新对手");

	public FindCompetitor(DataOutputStream dos, DataInputStream dis, Role role, int roleIndex, String sendData,
			Counter counter, JPanel panel) {

		ArrayList<Role> onlines = SerializeOperation.loadFromFile(sendData);
		
		ArrayList<Role> enemys = new ArrayList<Role>();
		for (Role r : onlines) {
			if (!r.getUserId().equals(counter.getId())) {
				enemys.add(r);
			}
		}
		
		setLayout(new GridLayout(1, 10));
		setFont(new Font("宋体", Font.BOLD, 16));

		ArrayList<JPanel> jpanels = new ArrayList<>();
		ArrayList<JButton> jButtons = new ArrayList<>();

		for (int i = 0; i < enemys.size(); i++) {
				JPanel jPanel = new JPanel();
				jPanel.setBackground(Color.white);
				jPanel.setLayout(new GridLayout(10, 1, 5, 5));
				jPanel.add(new JLabel("        昵称:" + enemys.get(i).getNickname()));
				jPanel.add(new JLabel("        职业名:" + enemys.get(i).getCareer().getClassify().getClassName() + "        "));
				jPanel.add(new JLabel("        生命值:" + enemys.get(i).getHeal() + "        "));
				jPanel.add(new JLabel("        魔法值:" + enemys.get(i).getImage() + "         "));
				jPanel.add(new JLabel("        攻击值:" + enemys.get(i).getAttack() + "         "));
				jPanel.add(new JLabel("        暴击值:" + enemys.get(i).getCritical() + "         "));
				jPanel.add(new JLabel("        防御值:" + enemys.get(i).getDefense() + "         "));
				jPanel.add(new JLabel("        速度值:" + enemys.get(i).getSpeed() + "          "));

				JButton jButton = new JButton("发起挑战");
				jPanel.add(jButton);
				jpanels.add(jPanel);
				jButtons.add(jButton);

			}
			for (JPanel jPanel : jpanels) {
				getContentPane().add(jPanel);
			}


		getContentPane().add(refresh);
		System.out.println(jButtons.size());
		for (int i = 0; i < jButtons.size(); i++) {
				Role enemy = enemys.get(i);
				jButtons.get(i).addActionListener(e -> {
				String str = "fight" + "#" + role.getRoleId() + "#" + enemy.getRoleId();
				String restr = sendData(str, panel, dos, dis);
				JFrame frame = new JFrame("PK界面");
				Mypanel mp = new Mypanel();
				PrintPerson pp = new PrintPerson();
				pp.placeComponents(mp, role, enemy, frame, dos, dis);
				frame.add(mp);
				frame.setSize(1200, 800);
				frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
				frame.setVisible(true);
				setVisible(false);
			});
		}

		refresh.addActionListener(e -> {
			String str = "inPKRoom#" + role.getRoleId();
			String restr = sendData(str,panel,dos, dis);
			String[] strs = restr.split("#"); 
			if(!strs[0].equals("startFight") ) { //没有对手选择
				reSelect(dos, dis, role, roleIndex, sendData, counter, panel);
			} else {
				startDualByEnemy(role, panel, enemys, strs, dos, dis);
			}
		});
	}

	//接受来自对手的挑战
	private void startDualByEnemy(Role role, JPanel panel, ArrayList<Role> enemys, String[] strs,DataOutputStream dos, DataInputStream dis) {
		//接受到挑战信息，开始决斗
		JOptionPane.showConfirmDialog(panel, "你收到一条PK信息，点击开始决斗", "开始决斗", JOptionPane.PLAIN_MESSAGE);
		setVisible(false);
		String enemyId = strs[1];
		Role enemy = null;
		for(Role r : enemys) {
			if(r.getRoleId().equals(enemyId)) {
				enemy = r;
				break;
			}
		}
		JFrame frame = new JFrame("PK界面");
		Mypanel mp = new Mypanel();
		PrintPerson pp = new PrintPerson();
		pp.placeComponents(mp, role, enemy, frame, dos ,dis);
		frame.add(mp);
		frame.setSize(1200, 800);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setVisible(true);
	}

	//重新选择
	private void reSelect(DataOutputStream dos, DataInputStream dis, Role role, int roleIndex, String sendData,
			Counter counter, JPanel panel) {
		setVisible(false);
		FindCompetitor gl=new FindCompetitor(dos, dis, role, roleIndex, sendData, counter, panel);
		gl.setTitle("对战房间");
		gl.pack();
		gl.setVisible(true);
		gl.setBounds(0, 0, 450, 400);
		gl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		gl.setLocationRelativeTo(null);
	}
	
	public String sendData(String str, JPanel panel,  DataOutputStream dos, DataInputStream dis) {
        try {
            dos.writeUTF(str);
            dos.flush();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(panel, "发送数据失败，可能连接已断开", "ERROR", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(0);
        }

        String retStr = null;

        try {
            retStr = dis.readUTF();
        } catch (Exception e) {
            JOptionPane.showMessageDialog(panel, "接收数据失败，可能连接已断开", "ERROR", JOptionPane.ERROR_MESSAGE);
            e.printStackTrace();
            System.exit(0);
        }
        return retStr;
	}
	// 测试
	public static void main(String[] args) {
		// RoleService roleSer = new RoleService();
		// ArrayList<Role> roles = new ArrayList<>();
		// roles.add(roleSer.findRoleById("859b46cc"));
		// roles.add(roleSer.findRoleById("1c40cb3a"));
		// FindCompetitor gl=new FindCompetitor(roles, null, null,);
		// gl.setTitle("对战房间");
		// gl.pack();
		// gl.setVisible(true);
		// gl.setBounds(0, 0, 450, 400);
		// gl.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		// gl.setLocationRelativeTo(null);

	}
}
